package com.foreknow.config;

import com.foreknow.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
import org.springframework.web.servlet.config.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * 跨域请求支持/token拦截
 * tip:只能写在一个配置类里
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    private TokenInterceptor tokenInterceptor;

    //构造方法
    public WebConfiguration(TokenInterceptor tokenInterceptor){
        this.tokenInterceptor = tokenInterceptor;

    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowedOrigins("*");
    }

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer){
        configurer.setTaskExecutor(new ConcurrentTaskExecutor(Executors.newFixedThreadPool(3)));
        configurer.setDefaultTimeout(30000);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry){
        List<String> excludePath = new ArrayList<>();
        //排除拦截
        excludePath.add("/user/register");  //登录
        excludePath.add("/user/login");     //注册
        excludePath.add("/user/test"); //测试
        excludePath.add("/user/findUserById");//根据id查询用户
        excludePath.add("/user/findUserByPage");//查询用户 分页
        excludePath.add("/user/searchUser");//高级搜索User
        excludePath.add("/user/deleteUserById");//根据id删除用户
        excludePath.add("/user/deleteUsersById");//批量删除用户
        excludePath.add("/user/modifyUser");//修改用户
        excludePath.add("/user/addUser");//添加用户
        excludePath.add("/department/findDepartment");//查询所有科室信息
        excludePath.add("/department/findDepartmentByPage");//根据页面查询科室信息
        excludePath.add("/department/findDeptById");//获取科室分类 中文
        excludePath.add("/department/deleteDeptById");//删除科室信息
        excludePath.add("/department/deleteDeptsById");//批量删除科室信息
        excludePath.add("/department/modifyDept");//修改科室信息
        excludePath.add("/department/addDept");//修改科室信息
        excludePath.add("/department/searchDept");//搜索
        excludePath.add("/registe/modify");//病例等级修改
        excludePath.add("/registe/findUser");//病例等级分页
        excludePath.add("/registe/findRegistById");//根据Id查询病例等级
        excludePath.add("/registe/deleteRegisteById");//根据id删除病例等级
        excludePath.add("/registe/insertRegiste");//增加病例等级
        excludePath.add("/registe/deleteRegiste");//批量删除病例
        excludePath.add("/registe/findregistelevel");//查询所有病例等级
        excludePath.add("/disease/findDisease");//分页查询疾病列表
        excludePath.add("/disease/findSDiseaseById");//根据id查询疾病
        excludePath.add("/disease/insertDisease");//增加病例
        excludePath.add("/disease/deleteDiseaseById");//根据id删除疾病
        excludePath.add("/disease/deleteDiseaseByIds");//批量删除
        excludePath.add("/disease/updateDiseaseById");//修改疾病
        excludePath.add("/disease/searchDisea");//高级搜索
        excludePath.add("/consItem/findConsItem");//分页查询常数项
        excludePath.add("/constype/findAllConsType");//查询所有常数项类型
        excludePath.add("/consItem/findConsItemById");//根据id查询常数
        excludePath.add("/consItem/insertConsItem");//增加常数项
        excludePath.add("/consItem/deleteConsItemById");//根据Id删除常数项
        excludePath.add("/consItem/deleteConsItemByIds");//批量删除常数项
        excludePath.add("/consItem/updateConsItemById");//修改常数项
        excludePath.add("/consItem/searchConsItem");//高级搜索常数项
        excludePath.add("/consItem/findConstantItem");//获取科室分类 中文
        excludePath.add("/scheduling//findSchedulingByPage");
        excludePath.add("/scheduling/addScheduling");
        excludePath.add("/rule/finRuleByPage");
        excludePath.add("/rule/addRules");
        excludePath.add("/static/**");  //静态资源
        excludePath.add("/assets/**");  //静态资源
        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePath);
        WebMvcConfigurer.super.addInterceptors(registry);
    }
}